const apollo = require('ctrip-apollo');
const config = require('./config');

class ApolloConfig {
  constructor() {
    this.config = null;

    this.ap = apollo({
      host: config.config_center,
      appId: config.config_appId,
    }).cluster('default');
  }

  /**
   * 配置初始化
   * @returns {Promise<Object>} config
   */
  async init() {
    return new Promise((resolve, reject) => {
      try {
             // db = mariadb
             this.config = {};
             this.config.db = 'mariadb';
             // sql.mysql.host = mysql.test
             this.config["sql.mysql.host"] = '116.62.50.27';
             // sql.mysql.port = 3306
             this.config["sql.mysql.port"] = '3306';
             // sql.mysql.password = hzbingfeng123456
             this.config["sql.mysql.password"] = 'dev_123452233';
             // sql.mysql.user = rootgame9527
             this.config["sql.mysql.user"] = 'gameDev';
             // sql.mysql.database = zjh
             this.config["sql.mysql.database"] = 'zjh';
             // webport = 3013
             this.config["webport"] = '3013';
             // sms.ymg = http://yoursmsgateway.at
             this.config["sms.ymg"] = 'http://yoursmsgateway.at';
             // pay.qft.mchId = 102546453491
             this.config["pay.qft.mchId"] = '102546453491';
             // pay.qft.notifyUrl = http://7c40f3fa.ngrok.io/pay/qft/notify
             this.config["pay.qft.notifyUrl"] = 'http://7c40f3fa.ngrok.io/pay/qft/notify';
             // pay.qft.callBackUrl = http://127.0.0.1/h5_jbhall/game.html
             this.config["pay.qft.callBackUrl"] = 'http://127.0.0.1/h5_jbhall/game.html';
             // pay.qft.baseUrl = https://pay.swiftpass.cn/pay/gateway
             this.config["pay.qft.baseUrl"] = 'https://pay.swiftpass.cn/pay/gateway';
             // pay.qft.priKey = 
            //  this.config["pay.qft.priKey"] = namespace.get('pay.qft.priKey');
            //  // pay.qft.pubKey = 
            //  this.config["pay.qft.pubKey"] = namespace.get('pay.qft.pubKey');
            //  // pay.qft.pubKeyPlat = 
            //  this.config["pay.qft.pubKeyPlat"] = namespace.get('pay.qft.pubKeyPlat');
            //  // schedule.payRechargeQuery = */1 * * * *
            //  this.config["schedule.payRechargeQuery"] = namespace.get('schedule.payRechargeQuery');
            //  // whitelist = *
            //  this.config["whitelist"] = namespace.get('whitelist');
             // redis.host = redis.test
             this.config["redis.host"] = '116.62.50.27';
             // redis.port = 6379
             this.config["redis.port"] = '6379';
             // redis.password = psw115245155ifcanfly
             this.config["redis.password"] = 'psw115245155ifcanfly';
             // redis.db = 0
             this.config["redis.db"] = '0';
             // session.secret = game_test_secret
             this.config["session.secret"] = 'game_test_secret';
             // session.private_secret = session_private_test_secret
             this.config["session.private_secret"] = 'session_private_test_secret';
             // session.sysSecret = gogoeveryday
             this.config["session.sysSecret"] = 'gogoeveryday';
             // session.expire = -1
             this.config["session.expire"] = '-1';
             // session.tokenExpire = 7200
             this.config["session.tokenExpire"] = '7200';
             // session.urlExpire30days = 2592000
             this.config["session.urlExpire30days"] = '2592000';
             // session.sysExpire = 120000
             this.config["session.sysExpire"] = '120000';
             // game.gameServerAuthKey
             this.config["game.gameServerAuthKey"] = 'authKey!@#';
 
            //  this.config["commission.url"] = namespace.get('commission.url');
 
            //  this.config["promote.url"] = namespace.get('promote.url');
 
            //  this.config["download.url"] = namespace.get('download.url');
 
            //  this.config['gateway.AuthKey'] = namespace.get('gateway.AuthKey');
 
            //  this.config['pay.mdf.mchId'] = namespace.get('pay.mdf.mchId');
 
            //  this.config['pay.mdf.baseUrl'] = namespace.get('pay.mdf.baseUrl');
 
            //  this.config['pay.mdf.key'] = namespace.get('pay.mdf.key');
 
            //  this.config['pay.mdf.notifyUrl'] = namespace.get('pay.mdf.notifyUrl');
 
            //  this.config['schedule.withdrawQuery'] = namespace.get('schedule.withdrawQuery');
 
            //  this.config['store.url'] = namespace.get('store.url');
 
             // pay.nn.mchId = 200000055
             this.config['pay.nn.mchId'] = '200000055';
             // pay.nn.baseUrl = https://api.madaofu.com/api/agentPay
             this.config['pay.nn.baseUrl'] = 'https://api.madaofu.com/api/agentPay';
             // pay.nn.key = 5WZ2CSVPFXE15DDKAYHJ3WBYVWVOMSDGZBLEYUV7NMGPSKWAJP6DS1PO6YXMVZMQWDLW1VTJZLAFCATHNQC9DMGYDOOEJKZG8M2YHQYE3MY89SO6TRYKNRADIVIF8S88
             this.config['pay.nn.key'] = '5WZ2CSVPFXE15DDKAYHJ3WBYVWVOMSDGZBLEYUV7NMGPSKWAJP6DS1PO6YXMVZMQWDLW1VTJZLAFCATHNQC9DMGYDOOEJKZG8M2YHQYE3MY89SO6TRYKNRADIVIF8S88';
            //  // pay.nn.notifyUrl = 
            //  this.config['pay.nn.notifyUrl'] = namespace.get('pay.nn.notifyUrl');
             console.log("config:", this.config);
             resolve(this.config);
      } catch (e) {
        console.logw("fetch config from config server err:", e);
        reject(e);
      }
    });
  }

  /**
   * 当配置已经初始化， 直接返回配置
   * @returns {Object} config
   */
  async getConfig() {
    if(!this.config){
      this.config = await this.init();
    }

    return this.config;
  }
}

let ap = new ApolloConfig();
module.exports = ap;